home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / libsrc / c / gen / fnmatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-30  |  3.7 KB  |  140 lines

  1. /* This is file FNMATCH.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1993 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1989 The Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * This code is derived from software contributed to Berkeley by
  12.  * Guido van Rossum.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #if defined(LIBC_SCCS) && !defined(lint)
  30. static char sccsid[] = "@(#)fnmatch.c    5.3 (Berkeley) 6/23/90";
  31. #endif /* LIBC_SCCS and not lint */
  32.  
  33. /*
  34.  * Function fnmatch() as proposed in Posix 1003.2 B.6 (rev. 9).
  35.  * Compares a filename or pathname to a pattern.
  36.  */
  37.  
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <fnmatch.h>
  41.  
  42. #define    EOS    '\0'
  43.  
  44. static char *
  45. rangematch(pattern, test)
  46.     register char *pattern, test;
  47. {
  48.   register char c, c2;
  49.   int negate, ok;
  50.  
  51.   if (negate = (*pattern == '!'))
  52.     ++pattern;
  53.  
  54.   /*
  55.    * TO DO: quoting
  56.    */
  57.  
  58.   for (ok = 0; (c = *pattern++) != ']';) {
  59.     if (c == EOS)
  60.       return(NULL);        /* illegal pattern */
  61.     if (*pattern == '-' && (c2 = pattern[1]) != EOS && c2 != ']') {
  62.       if (c <= test && test <= c2)
  63.     ok = 1;
  64.       pattern += 2;
  65.     }
  66.     else if (c == test)
  67.       ok = 1;
  68.   }
  69.   return(ok == negate ? NULL : pattern);
  70. }
  71.  
  72. int
  73. fnmatch(pattern, string, flags)
  74.     const char *pattern, *string;
  75.     int flags;
  76. {
  77.   register char c;
  78.   char test, *rangematch();
  79.  
  80.   for (;;)
  81.     switch (c = *pattern++) {
  82.     case EOS:
  83.       return(*string == EOS ? 0 : FNM_NOMATCH);
  84.     case '?':
  85.       if ((test = *string++) == EOS ||
  86.       test == '/' && flags & FNM_PATHNAME)
  87.     return(FNM_NOMATCH);
  88.       break;
  89.     case '*':
  90.       c = *pattern;
  91.       /* collapse multiple stars */
  92.       while (c == '*')
  93.     c = *++pattern;
  94.  
  95.       /* optimize for pattern with * at end or before / */
  96.       if (c == EOS)
  97.     if (flags & FNM_PATHNAME)
  98.       return(index(string, '/') ? FNM_NOMATCH : 0);
  99.     else
  100.       return(0);
  101.       else if (c == '/' && flags & FNM_PATHNAME) {
  102.     if ((string = index(string, '/')) == NULL)
  103.       return(FNM_NOMATCH);
  104.     break;
  105.       }
  106.  
  107.       /* general case, use recursion */
  108.       while ((test = *string) != EOS) {
  109.     if (fnmatch(pattern, string, flags) == 0)
  110.       return(0);
  111.     if (test == '/' && flags & FNM_PATHNAME)
  112.       break;
  113.     ++string;
  114.       }
  115.       return(FNM_NOMATCH);
  116.     case '[':
  117.       if ((test = *string++) == EOS ||
  118.       test == '/' && flags & FNM_PATHNAME)
  119.     return(FNM_NOMATCH);
  120.       if ((pattern = rangematch(pattern, test)) == NULL)
  121.     return(FNM_NOMATCH);
  122.       break;
  123.     case '\\':
  124.       if (flags & FNM_QUOTE) {
  125.     if ((c = *pattern++) == EOS) {
  126.       c = '\\';
  127.       --pattern;
  128.     }
  129.     if (c != *string++)
  130.       return(FNM_NOMATCH);
  131.     break;
  132.       }
  133.       /* FALLTHROUGH */
  134.     default:
  135.       if (c != *string++)
  136.     return(FNM_NOMATCH);
  137.       break;
  138.     }
  139. }
  140.